package epimarket.beans;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import epimarket.beans.Product;

public class ProductPeer {
  public static ArrayList searchProducts(DataManager dataManager,
    String keyword) {
    ArrayList<Product> products = new ArrayList<Product>();
    Connection connection = dataManager.getConnection();
    if (connection != null) {
      try {
        Statement s = connection.createStatement();
        String sql = "select product_id, name, description, price from products"
            + " where name like '%" + keyword.trim() + "%'"
            + " or description like '%" + keyword.trim() + "%'";
        try {
          ResultSet rs = s.executeQuery(sql);
          try {
            while (rs.next()) {
              Product product = new Product();
              product.setId(rs.getString(1));
              product.setName(rs.getString(2));
              product.setDescription(rs.getString(3));
              product.setPrice(rs.getDouble(4));
              products.add(product);
              }
            }
          finally { rs.close(); }
          }
        finally { s.close(); }
        }
      catch (SQLException e) {
        System.out.println("Could not search for products:" + e.getMessage());
        }
      finally {
        dataManager.putConnection(connection);
        }
      }
    return products;
    }
	
  public static ArrayList getProductsByCategory(DataManager dataManager,
    String categoryId) {
    ArrayList<Product> products = new ArrayList<Product>();
    Connection connection = dataManager.getConnection();
    if (connection != null) {
      try {
        Statement s = connection.createStatement();
        String sql = "select product_id, name, description, price from products"
            + " where category_id=" + categoryId;
        try {
          ResultSet rs = s.executeQuery(sql);
          try {
            while (rs.next()) {
              Product product = new Product();
              product.setId(rs.getString(1));
              product.setName(rs.getString(2));
              product.setDescription(rs.getString(3));
              product.setPrice(rs.getDouble(4));
              products.add(product);
              }
            }
          finally { rs.close(); }
          }
        finally { s.close(); }
        }
      catch (SQLException e) {
        System.out.println("Could not get products: " + e.getMessage());
        }
      finally {
        dataManager.putConnection(connection);
        }
      }
      return products;
    }

  public static Product getProductById(DataManager dataManager, String productID) {
    Product product = null;
    Connection connection = dataManager.getConnection();
    if (connection != null) {
      try {
        Statement s = connection.createStatement();
        String sql = "select product_id, name, description, price from products"
            + " where product_id=" + productID;
        try {
          ResultSet rs = s.executeQuery(sql);
          try {
            while (rs.next()) {
              product = new Product();
              product.setId(rs.getString(1));
              product.setName(rs.getString(2));
              product.setDescription(rs.getString(3));
              product.setPrice(rs.getDouble(4));
              }
            }
          finally { rs.close(); }
          }
        finally { s.close(); }
        }
      catch (SQLException e) {
        System.out.println("Could not get product: " + e.getMessage());
        }
      finally {
        dataManager.putConnection(connection);
        }
      }
    return product;
    }
  }
